package com.hong.servicehosp.controller.api;

import com.alibaba.excel.util.StringUtils;
import com.hong.commonutil.exception.YyghException;
import com.hong.commonutil.result.Result;
import com.hong.commonutil.result.ResultCodeEnum;
import com.hong.model.model.hosp.Hospital;
import com.hong.model.model.hosp.Schedule;
import com.hong.model.vo.hosp.HospitalQueryVo;
import com.hong.model.vo.hosp.ScheduleOrderVo;
import com.hong.model.vo.order.SignInfoVo;
import com.hong.servicehosp.service.HospitalService;
import com.hong.servicehosp.service.HospitalSetService;
import com.hong.servicehosp.service.ScheduleService;
import com.hong.serviceutil.common.helper.HttpRequestHelper;
import com.hong.serviceutil.utils.MD5;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/api/hosp/")
public class ApiHospitalController {
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private ScheduleService scheduleService;
    @ApiOperation("查询医院")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        //根据传递过来的医院编码，查询数据库，查询签名
        String hostCode = (String) paramMap.get("hoscode");
        String hospSign = (String) paramMap.get("sign");
        hospitalSetService.checkHospSign(hostCode, hospSign);
        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hostCode);
        return Result.ok(hospital);
    }

    @ApiOperation("上传医院接口")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        //获取医院系统传递过来的签名并进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //根据传递过来的医院编码，查询数据库，查询签名
        String hostCode = (String) paramMap.get("hoscode");
        hospitalSetService.checkHospSign(hostCode, hospSign);
        //传输过程中"+"转成了" "，因此我们要转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);
        hospitalService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation("查询医院列表")
    @GetMapping("hospital/findHospList/{page}/{limit}")
    public Result findHospList(@PathVariable int page, @PathVariable int limit, HospitalQueryVo hospitalQueryVo) throws ExecutionException, InterruptedException {
        Page<Hospital> hospitals = hospitalService.selectHospitalByPage(page, limit, hospitalQueryVo);
        return Result.ok(hospitals);
    }

    @ApiOperation("根据医院名称做模糊查询")
    @GetMapping("hospital/findByHosName/{hosname}")
    public Result findByHosName(@PathVariable String hosname) {
        List<Hospital> list = hospitalService.findByHosName(hosname);
        return Result.ok(list);
    }

    @ApiOperation("根据医院编号获取医院的预约挂号详情")
    @GetMapping("hospital/findHospDetail/{hoscode}")
    public Result findHospDetail(@PathVariable String hoscode) throws InterruptedException, ExecutionException {
        Map<String, Object> hospital = hospitalService.findHospDetail(hoscode);
        return Result.ok(hospital);
    }
    @ApiOperation("获取可预约排班数据")
    @GetMapping("hospital/auth/getBookingScheduleRule/{page}/{limit}/{hoscode}/{depcode}")
    public Result getBookingSchedule(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Integer page,
                                     @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Integer limit,
                                     @ApiParam(name = "hoscode", value = "医院code", required = true) @PathVariable String hoscode,
                                     @ApiParam(name = "depcode", value = "科室code", required = true) @PathVariable String depcode) {
        return Result.ok(scheduleService.getBookingScheduleRule(page, limit, hoscode, depcode));
    }

    /**
     * 根据 hoscode, depcode, workDate查询排班数据
     * @param hoscode
     * @param depcode
     * @param workDate
     * @return
     */
    @ApiOperation("获取排班具体数据")
    @GetMapping("hospital/auth/findScheduleList/{hoscode}/{depcode}/{workDate}")
    public Result findScheduleList(
            @ApiParam(name = "hoscode", value = "医院code", required = true) @PathVariable String hoscode,
            @ApiParam(name = "depcode", value = "科室code", required = true) @PathVariable String depcode,
            @ApiParam(name = "workDate", value = "排班日期", required = true) @PathVariable String workDate) {
        return Result.ok(scheduleService.getDetailSchedule(hoscode, depcode, workDate));
    }
    @ApiOperation("根据排班id获取预约下单数据")
    @GetMapping("hospital/inner/getScheduleOrderVo/{scheduleId}")
    public ScheduleOrderVo getScheduleOrderVo(@ApiParam(name = "scheduleId", value = "排班id", required = true)
                                              @PathVariable("scheduleId") String scheduleId){
        return scheduleService.getScheduleOrderVo(scheduleId);
    }
    @ApiOperation("获取医院签名信息")
    @GetMapping("hospital/inner/getSignInfoVo/{hoscode}")
    public SignInfoVo getSignInfoVo(@ApiParam(name = "hoscode", value = "医院code", required = true)
                                    @PathVariable("hoscode") String hoscode){
        return hospitalSetService.getSignInfoVo(hoscode);
    }
}
